import { _decorator, Animation, AnimationClip, AnimationState } from "cc";
import State from "./Base/State";
import StateMachine, { getInitParamsTrigger } from "./Base/StateMachine";
import { EState, EntityType, EParamsName } from "./Config/Enum";
//import { PetConfig } from "./Config/PetConfig";
const { ccclass } = _decorator;

/**
 * 宠物状态机类，继承自StateMachine，用于管理宠物的各种状态和动画
 */
@ccclass("PetStateMachine")
export class PetStateMachine extends StateMachine {

  // 动画播放速度
  animationSpeed = 0.25
  // 攻击速度
  attackSpeed = 0.1


  /**
   * 初始化方法
   * @param type 实体类型
   */
  init(type: EntityType) {
    this.type = type;
    this.animationComponent = this.node.addComponent(Animation);
    //this.attackSpeed = attackSpeed
    // 获取动画组件，如果不存在则添加
    //this.animationComponent = this.node.getChildByName('Animation').getComponent(Animation) || this.node.getChildByName('Animation').addComponent(Animation);
    this.initParams();
    this.initStateMachines();
    this.initAnimationEvent();
    // const enemyConfig = PetConfig[type];
    // this.attackSpeed = enemyConfig.attackSpeed;
  }

  initParams() {
    this.params.set(EParamsName.Idle, getInitParamsTrigger());
    //this.params.set(EParamsName.Run, getInitParamsTrigger());
    this.params.set(EParamsName.Attack, getInitParamsTrigger());
    this.params.set(EParamsName.IdleFront, getInitParamsTrigger());
  }

  initStateMachines() {
    this.stateMachines.set(EParamsName.Idle, new State(this, `${this.type}${EState.Idle}`, AnimationClip.WrapMode.Normal,false, this.animationSpeed));
    //this.stateMachines.set(EParamsName.Run, new State(this, `${this.type}${EState.Run}`, AnimationClip.WrapMode.Loop, false, this.animationSpeed));
    this.stateMachines.set(EParamsName.Attack, new State(this, `${this.type}${EState.Attack}`,   AnimationClip.WrapMode.Normal,false, this.animationSpeed));
    this.stateMachines.set(EParamsName.IdleFront, new State(this, `${this.type}${EState.IdleFront}`,   AnimationClip.WrapMode.Loop,false, this.animationSpeed));
  }

  // initAnimationEvent() {}



  initAnimationEvent() {
    this.animationComponent.on(Animation.EventType.FINISHED, (type, state) => {
        this.onAnimationFinished(type, state);
    }, this);
}


  onAnimationFinished(type: Animation.EventType, state: AnimationState) {
    if (state.name.includes(EState.Die)) {
      // 死亡动画播放完毕，触发销毁逻辑
      this.node.emit("PetDieAnimationFinished");
    }else if (state.name.includes(EState.Attack)) {
      // 攻击动画播放完毕，触发攻击逻辑
      this.node.emit("PetAttackAnimationFinished");
    }
  }


  run() {
    switch (this.currentState) {
      // case this.stateMachines.get(EParamsName.Run):
      //   if (this.params.get(EParamsName.Attack).value) {
      //     this.currentState = this.stateMachines.get(EParamsName.Attack);
      //   } else if (this.params.get(EParamsName.Die).value) {
      //     this.currentState = this.stateMachines.get(EParamsName.Die);
      //   } else if (this.params.get(EParamsName.Idle).value) {
      //     this.currentState = this.stateMachines.get(EParamsName.Idle);
      //   } else {
      //     this.currentState = this.currentState;
      //   }
      //   break;
      case this.stateMachines.get(EParamsName.Idle):
        if (this.params.get(EParamsName.Attack).value) {
          this.currentState = this.stateMachines.get(EParamsName.Attack);
        } else if (this.params.get(EParamsName.IdleFront).value) {
          this.currentState = this.stateMachines.get(EParamsName.IdleFront);
        } else {
          this.currentState = this.currentState;
        }
        break;

      case this.stateMachines.get(EParamsName.Attack):
        if (this.params.get(EParamsName.IdleFront).value) {
          this.currentState = this.stateMachines.get(EParamsName.IdleFront);
        } else if (this.params.get(EParamsName.Idle).value) {
          this.currentState = this.stateMachines.get(EParamsName.Idle);
        } else {
          this.currentState = this.currentState;
        }
        break;

      case this.stateMachines.get(EParamsName.IdleFront):
        if (this.params.get(EParamsName.Attack).value) {
          this.currentState = this.stateMachines.get(EParamsName.Attack);
        }else if (this.params.get(EParamsName.Idle).value) {
          this.currentState = this.stateMachines.get(EParamsName.Idle);
        } else {
          this.currentState = this.currentState;
        }
        break;
      
      // case this.stateMachines.get(EParamsName.Die):
      //   break;
      default:
        this.currentState = this.stateMachines.get(EParamsName.Idle);
        break;
    }
  }
}
